/**
 * Created by Administrator on 2018/11/13 0013.
 */
/**
 * Created by john on 2018/3/28.
 */
var express = require('express');
var router = express.Router();
var person = require("../src/person");
var CONF = require("../config/conf");
var configVar = require("../config/code");
/*pc端页面得路由*/
/* GET home page. */

router.use(function (req, res, next) {
    if(req.session.userinfo){
        if( req.session.userinfo.type == configVar.type.admin){
            next();
        }else{
            res.status(403).send("禁止访问");
        }
    }else{
        var url = CONF.ssoLogin;
        var cd = res.locals._fromUrl;
        console.log(req._fromUrl);
        url = url + cd;
        res.redirect(url);
    }

})

router.get('/', function(req, res, next) {
    person.getRoot().then(function (ret) {
        res.render('pc/index', ret);
    }).catch(function (ret) {
        res.sendStatus(404);
        next();
    });
});
router.get('/add/:id/:name', function(req, res, next) {
    res.render('pc/add',req.params);
});
router.get('/info/:id', function(req, res, next) {
    person.findDetailById(req.params).then(function (ret) {
        console.log(ret)
        res.render('pc/info',ret);
    }).catch(function (ret) {
        res.sendStatus(404);
        next();
    })
});

router.get('/root/:id', function(req, res, next) {
    var id = req.params.id || '';
    if(id){
        person.getById({ids:[id]}).then(function (ret) {
            res.render('pc/root', ret);
        }).catch(function (ret) {
            res.sendStatus(404);
            next();
        });
    }else{
        res.sendStatus(404);
        next();
    }
});


module.exports = router;
